package com.neusoft.yuejian_01.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.UserBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.UserModel;
import com.neusoft.yuejian_01.util.CacheInfo;


public class LoginActivity extends BaseActivity implements RetrofitListener {
    private TextView text_reg;
    private Button btn_log;
    private  EditText username;
    private  EditText userpass;
    private  CheckBox checkPassword;
    private boolean flag;

    //连接xml布局控件
    protected void initView() {
        setContentView(R.layout.activity_login);
        btn_log=(Button)findViewById(R.id.button_login);
        username=(EditText)findViewById(R.id.editText_name);
        userpass=(EditText)findViewById(R.id.editText_password);
        text_reg=(TextView) findViewById(R.id.text_region);
        checkPassword=(CheckBox)findViewById(R.id.rememberpass);
        checkPassword.setOnCheckedChangeListener(myCheckChangelistener);
        flag = new CacheInfo(this).getPassRememberCache();
        setListener();
    }
    //对是否记住密码进行监听，
    private void setListener() {
       checkPassword.setOnCheckedChangeListener(myCheckChangelistener);
    }

  CompoundButton.OnCheckedChangeListener myCheckChangelistener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked){
                checkPassword.setChecked(true);
                new CacheInfo(LoginActivity.this).setPassRememberCache(true);
            }else{
                checkPassword.setChecked(false);
                new CacheInfo(LoginActivity.this).setPassRememberCache(false);
            }

        }
    };


//判断是否定义成功，以及是否选择记住密码方式

   protected void initEvents(){
        isLogin();
        checkPassword.setChecked(flag);
        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel = new UserModel();
                userModel.login(username.getText().toString(), userpass.getText().toString(), LoginActivity.this);
            }
        });
        text_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void initData() {

    }


    //登陆成功后，判断是否选择记住密码，显示相应的数据在控件上
    private void isLogin(){
       /* new CacheInfo(this).getUserIdCache();*/
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String username=sharedPreferences.getString("username",null);
        String userpass=sharedPreferences.getString("userpass",null);
        if(username!=null&&userpass!=null){
            this.username.setText(username);
            if(flag) {
                this.userpass.setText(userpass);
            }
        }
    }
//保存相应的信息
    public  void  saveUser(int user_id,int c_id){
            SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器,共享数据
            editor.putString("username", username.getText().toString());
            editor.putString("userpass", userpass.getText().toString());
            int userid = Integer.parseInt(String.valueOf(user_id));
            int cid = Integer.parseInt(String.valueOf(c_id));
            editor.putInt("u_id", userid);
            editor.putInt("c_id", cid);
            editor.putString("userpass", userpass.getText().toString());
            editor.commit();//提交修改
        }
    @Override
    public void onSuccess(Object object, int flag) {
        UserBean userBean = (UserBean)object;
        int u_id = userBean.getU_id();
        int c_id= userBean.getU_id();
        //失败处理
        if("0".equals(u_id)) {
            Toast.makeText(LoginActivity.this, "用户或密码错误", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            saveUser(u_id,c_id);
            Intent intent=new Intent();
            intent.setClass(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            //成功处理
        }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();
    }
}



